package com.tiantian.quanminxiu.adapter;

import android.app.Activity;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.tiantian.quanminxiu.R;
import com.tiantian.quanminxiu.activity.Activity_FanRoll;
import com.tiantian.quanminxiu.utils.APPUtils;

import org.json.JSONArray;
import org.json.JSONObject;

/**
 * Created by admin on 2016/1/14.
 */
public class FanRollAdapter extends RecyclerView.Adapter<FanRollAdapter.MyViewHolder>
{
    private String Type;

    private Activity activity;
    private JSONArray list;
    public FanRollAdapter(Activity activity, JSONArray list, String Type){
        this.activity = activity;
        this.list = list;
        this.Type = Type;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(
                activity).inflate(R.layout.activity_fanroll_fanitem, parent,
                false));
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position)
    {
        JSONObject user = list.optJSONObject(position);
        holder.txtUserRanking.setTextColor(Color.parseColor("#a3a3a3"));
        if(position<3){
            holder.txtUserRanking.setTextColor(APPUtils.getResourcesColor(activity,R.color.orange));
        }
        holder.txtUserRanking.setText((position+1)+"");
        holder.txtUserName.setText(user.optString("NickName"));
        holder.imgUserAvatar.post(new Runnable() {
            @Override
            public void run() {
                APPUtils.displayImage(APPUtils.createUserAvatar(user.optInt("User_ID")), holder.imgUserAvatar, activity.getBaseContext(), holder.imgUserAvatar.getHeight() / 2);
            }
        });
        if(Activity_FanRoll.TOTAL.equalsIgnoreCase(Type)){
            holder.txtUserContribution.setText(user.optString("Coin"));
        }else if(Activity_FanRoll.WEEK.equalsIgnoreCase(Type)){
            holder.txtUserContribution.setText(user.optString("Coin_Week"));
        }else {
            holder.txtUserContribution.setText(user.optString("Coin_Live"));
        }
        holder.vContent.setTag(user);
        holder.vContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                APPUtils.Dialog_UserInfoCard_To(activity,user.optInt("User_ID"),user.optString("NickName"),-1,user.optInt("Level"),-1,false,false);
            }
        });
    }

    @Override
    public int getItemCount()
    {
        return list.length();
    }

    class MyViewHolder extends RecyclerView.ViewHolder
    {
        TextView txtUserRanking,txtUserName,txtUserContribution;
        ImageView imgUserAvatar;
        View vContent;

        public MyViewHolder(View view)
        {
            super(view);
            vContent = view.findViewById(R.id.vContent);
            imgUserAvatar = (ImageView) view.findViewById(R.id.imgUserAvatar);
            txtUserRanking = (TextView) view.findViewById(R.id.txtUserRanking);
            txtUserName = (TextView) view.findViewById(R.id.txtUserName);
            txtUserContribution = (TextView) view.findViewById(R.id.txtUserContribution);
        }
    }

}
